package ah.file;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsICharsetDetectionObserver;

import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;

public class CPDetectorTest {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		String fileName = "/home/ddh/workspace/MyTest/webapp/pages/tomcat/tomcat配置.txt";
		CPDetectorTest cpd = new CPDetectorTest();
		System.out.println(cpd.method(fileName));
		System.out.println(cpd.method2(fileName));
		
	}

	/**
	 * cpdetector
	 * @throws Exception 
	 */
	public String method(String fileName) throws Exception {
		/*------------------------------------------------------------------------
		detector是探测器，它把探测任务交给具体的探测实现类的实例完成。
		cpDetector内置了一些常用的探测实现类，这些探测实现类的实例可以通过add方法
		加进来，如ParsingDetector、 JChardetFacade、ASCIIDetector、UnicodeDetector。  
		detector按照“谁最先返回非空的探测结果，就以该结果为准”的原则返回探测到的
		字符集编码。cpDetector是基于统计学原理的，不保证完全正确
		--------------------------------------------------------------------------*/
		CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
		// ParsingDetector可用于检查HTML、XML等文件或字符流的编码
		detector.add(new ParsingDetector(false));// 显示探测过程的详细信息
		// ASCIIDetector用于ASCII编码测定
		detector.add(ASCIIDetector.getInstance());
		// UnicodeDetector用于Unicode家族编码的测定
		detector.add(UnicodeDetector.getInstance());
		// JChardetFacade封装了由Mozilla组织提供的JChardet
		detector.add(JChardetFacade.getInstance());
		java.nio.charset.Charset charset = null;
		File f = new File(fileName);
		FileInputStream fis = new FileInputStream(f);
		BufferedInputStream bis = new BufferedInputStream(fis);
		long length = fis.available();
		try {
			long start = System.currentTimeMillis();
			charset = detector.detectCodepage(f.toURL());
			//charset = detector.detectCodepage(bis, length>600?600:(int)length);
			long end = System.currentTimeMillis();
			//System.out.println(end-start);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			fis.close();
			bis.close();
		}
		if (charset != null) {
			//System.out.println(f.getAbsolutePath() + "method 编码是：" + charset.name());
		} else {
			//System.out.println(f.getAbsolutePath() + " method 未知");
		}
		
		return charset.toString();
	}

	/**
	 * JChardet
	 */
	public String method2(String fileName) {
		// Initalize the nsDetector() ;
		nsDetector det = new nsDetector(nsDetector.SIMPLIFIED_CHINESE);// 2为中文,3简体,4传统,6 default donot know
		// Set an observer...
		// The Notify() will be called when a matching charset is found.
		final String[] charsetResult = new String[1];
		det.Init(new nsICharsetDetectionObserver() {
			public void Notify(final String charset) {
				charsetResult[0] = charset;
			}
		});
		
		BufferedInputStream imp = null;
		File f = new File(fileName);
		if (f.length() == 0) {
			return "US-ASCII";
		}
		try {
			imp = new BufferedInputStream(new FileInputStream(f));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		byte[] buf = new byte[4096];
		int len;
		boolean done = false;
		boolean isAscii = true;

		try {
			long start = System.currentTimeMillis();
			while ((len = imp.read(buf, 0, buf.length)) != -1) {
				// Check if the stream is only ascii.
				if (isAscii) {
					isAscii = det.isAscii(buf, len);
					if (isAscii) {
						charsetResult[0] = "US-ASCII";
					}
				}
				// DoIt if non-ascii and not done yet.
				if (!isAscii && !done) {
					done = det.DoIt(buf, len, false);
				}
			}
			long end = System.currentTimeMillis();
			//System.out.println(end-start);
		} catch (IOException e) {
			e.printStackTrace();
		}
		det.DataEnd();

		if (charsetResult[0] != null) {
			//System.out.println(f.getAbsolutePath() + "method2 编码是：" + charsetResult[0]);
		} else {
			//System.out.println(f.getAbsolutePath() + " method2 未知");
		}
		return charsetResult[0];
	}
	
	public String tikaDetector() {
		//CharsetDetector 
		return null;
	}
	
	
}
